package com.approval.approvalapp.index.cygn.wzly;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.WzlyListBean;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by louyulin on 2019/5/6.
 */

public class LrvAdapter extends RecyclerView.Adapter<LrvAdapter.Holder> {
    List<WzlyListBean> wzlyListBeanList;
    Context context;
    ClickItemLrvListener listener;


    public void setWzlyListBeanList(List<WzlyListBean> wzlyListBeanList,
                                    ClickItemLrvListener listener) {
        this.wzlyListBeanList = wzlyListBeanList;
        this.listener = listener;
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_lrv, parent, false);
        Holder holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {
            if (wzlyListBeanList.get(position).isSelect()){
                holder.view.setVisibility(View.VISIBLE);
                holder.tv.setTextColor(Color.parseColor("#1465FD"));
            }else {
                holder.view.setVisibility(View.INVISIBLE);
                holder.tv.setTextColor(Color.parseColor("#7D8198"));
            }
            holder.tv.setText(wzlyListBeanList.get(position).getName());
            if (wzlyListBeanList.get(position).isSelect()){
                holder.root.setBackgroundColor(Color.parseColor("#ffffff"));
            }else {
                holder.root.setBackgroundColor(Color.parseColor("#f5f5f5"));
            }
            holder.root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.clickItem(position);
                }
            });
            if (wzlyListBeanList.get(position).getRedcount() == 0){
                holder.redTv.setVisibility(View.GONE);
            }else {
                holder.redTv.setVisibility(View.VISIBLE);
                holder.redTv.setText(wzlyListBeanList.get(position).getRedcount() + "");
            }
    }

    @Override
    public int getItemCount() {
        if (wzlyListBeanList == null) {
            return 0;
        } else {
            return wzlyListBeanList.size();
        }
    }

    class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.view)
        View view;
        @Bind(R.id.tv)
        TextView tv;
        @Bind(R.id.redTv)
        TextView redTv;
        @Bind(R.id.root)
        LinearLayout root;
        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface ClickItemLrvListener{
        public void clickItem(int position);
    }


}
